xquery version "3.0";
import module namespace ddi-exist-utils ='http://code.google.com/p/ddi-exist/'       at '/db/ddi/lib/utils.xqm';
import module namespace ddi-exist       ='http://code.google.com/p/ddi-exist/search' at '/db/ddi/lib/search.xqm';

import module namespace request="http://exist-db.org/xquery/request";
import module namespace json="http://www.json.org";
import module namespace util="http://exist-db.org/xquery/util";

let $config := doc('xmldb:exist:///db/ddi/config.xml')

(: let $collection := collection($config/collection) :)

let $collection := collection('/db/ddi/data/ddi3/')

(: if callback is defined :)
(: let $callback := request:get-parameter("callback", ()) :)

let $lang := request:get-parameter("lang", ())

let $q := request:get-parameter("q", ())
let $series := xs:string(request:get-parameter("series", ()))

let $start    := xs:integer(request:get-parameter("start", "0"))
let $records  := xs:integer(request:get-parameter("records", 10))
let $action   := request:get-parameter("action","status")
let $format   := request:get-parameter("format","xml")
let $callback := request:get-parameter("callback",())
let $type     := request:get-parameter("type","status,study,question,variable")

let $query-start-time := util:system-time()

(:set the correct header for the requested format:)
let $null := 
    if($format = 'xml') 
    then
        util:declare-option("exist:serialize", "method=xml media-type=text/xml")
    else if($format = 'json' and empty($callback))
        then
        util:declare-option("exist:serialize", "method=json media-type=application/json")
    else if($format = 'json')
        then
        util:declare-option("exist:serialize", fn:concat("method=json media-type=application/json jsonp=", $callback))
    else
        util:declare-option("exist:serialize", "method=xml media-type=text/xml")

(:get the status of the current document:)
let $status := ddi-exist-utils:status($collection)

(: preform the search:)
let $questions := ddi-exist:searchQuestion($q, $lang, $collection)
let $variables := ddi-exist:searchVariable($q, $lang, $collection)
let $studies   := ddi-exist:searchStudy($q, $lang, $collection)

(:limit the matches:)
let $questionsLimited := ddi-exist:limitMatches($questions, $start, $records) 
let $variablesLimited := ddi-exist:limitMatches($variables, $start, $records) 
let $studiesLimited   := ddi-exist:limitMatches($studies,   $start, $records) 

(:time to run the query:)
let $time := (util:system-time() - $query-start-time) div xs:dayTimeDuration('PT1S')

return 
<result>
    {
        <time>{$time}</time>,
        <start>{$start}</start>,
        <end>{($start + $records)}</end>,
        $status
    }
    <studies hits="{count($studies)}">{$studiesLimited}</studies>
    <questions hits="{count($questions)}">{$questionsLimited}</questions>
    <variables hits="{count($variablesLimited)}">{$variablesLimited}</variables>
</result>